﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.ExportToRTR.CApExportToRTRUserMessages
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using DnProcessSimulateCommands.ExportToRTR.Resources;
using EngineeringInternalExtension;
using System.Collections.Generic;
using System.IO;
using System.Text;


namespace DnProcessSimulateCommands.ExportToRTR{

internal class CApExportToRTRUserMessages
{
  private StringBuilder m_content;

  public TxReportWriterEx LogWriter { get; private set; }

  public CApExportToRTRLocationsInfo LocationsInfo { get; private set; }

  public CApExportToRTRUserMessages(string path)
  {
    this.LogWriter = new TxReportWriterEx(path);
    this.m_content = new StringBuilder();
    this.LocationsInfo = new CApExportToRTRLocationsInfo();
  }

  internal bool IsEmpty()
  {
    return this.LocationsInfo.IsEmpty() && this.m_content.Length == 0 && new FileInfo(this.LogWriter.ReportFileFullPath).Length == 0L;
  }

  internal void Show()
  {
    if (!this.LocationsInfo.IsEmpty())
    {
      this.AppendFormatMessage(this.LocationsInfo.DuplicateCounter, ExportToRTRStringTable.LOCATIONS_NOT_UNIQUE_WARNING_MSG, ExportToRTRStringTable.LOCATION, ExportToRTRStringTable.LOCATIONS);
      this.AppendFormatMessage(this.LocationsInfo.InvalidCounter, ExportToRTRStringTable.LOCATIONS_NOT_VALID_ERROR_MSG, ExportToRTRStringTable.LOCATION, ExportToRTRStringTable.LOCATIONS);
      this.AppendFormatMessage(this.LocationsInfo.NonConfiguredCounter, ExportToRTRStringTable.LOCATION_NOT_CONFIGURED_ERROR_MSG, ExportToRTRStringTable.LOCATION, ExportToRTRStringTable.LOCATIONS);
      this.m_content.AppendLine();
    }
    else
      this.m_content.AppendLine(ExportToRTRStringTable.LOG_ISSUES);
    this.m_content.AppendLine(ExportToRTRStringTable.OPEN_LOG_FILE);
    this.LogWriter.ShowOpenReportFileForm(this.m_content.ToString(), CCoExportToRTRCmd.CmdName);
    this.LogWriter.Dispose();
  }

  internal void RemoveLog()
  {
    try
    {
      this.LogWriter.Dispose();
      File.Delete(this.LogWriter.ReportFileFullPath);
    }
    catch
    {
    }
  }

  internal void ValidateUniqueNames(CApExportToRTRXmlWriter writer)
  {
    this.DuplicationValidation(writer.Joints, ExportToRTRStringTable.JOINT_HEADER, ExportToRTRStringTable.JOINT, ExportToRTRStringTable.JOINTS);
    this.DuplicationValidation(writer.Links, ExportToRTRStringTable.LINK_HEADER, ExportToRTRStringTable.LINK, ExportToRTRStringTable.LINKS);
  }

  private string CreateUserMessage(int counter, string format, string single, string plural)
  {
    string str = counter == 1 ? single : plural;
    return string.Format(format, (object) counter, (object) str);
  }

  private void AppendFormatMessage(int counter, string format, string single, string plural)
  {
    if (counter <= 0)
      return;
    this.m_content.AppendLine(this.CreateUserMessage(counter, format, single, plural));
  }

  private void DuplicationValidation(
    Dictionary<string, int> counter,
    string header_name,
    string single,
    string plural)
  {
    int counter1 = 0;
    foreach (KeyValuePair<string, int> keyValuePair in counter)
    {
      if (keyValuePair.Value > 1)
      {
        ++counter1;
        this.LogWriter.WriteLine(string.Format(ExportToRTRStringTable.NOT_UNIQUE_WARNING_LOG, (object) header_name, (object) keyValuePair.Key, (object) keyValuePair.Value));
      }
    }
    this.AppendFormatMessage(counter1, ExportToRTRStringTable.NOT_UNIQUE_WARNING_MSG, single, plural);
  }
}
